package com.abm;

import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class AskIdActivity extends Activity {

	JSONArray respJSON;
	List<String> idList;
	Spinner spnr;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ask_id);
		
		spnr = new Spinner(this);
		spnr = (Spinner) findViewById(R.id.spnSelectId);
		
		Button btnUpdate = new Button(this);
		btnUpdate = (Button) findViewById(R.id.btnUpdate);
		btnUpdate.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				new LongRunningGetIO().execute();
			}
		});

		Button btnSelect = new Button(this);
		btnSelect = (Button) findViewById(R.id.btnSelect);
		btnSelect.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO: Intent que pasa el id seleccionado a RestActivity
			}
		});
		
	}





	@SuppressWarnings("unused")
	private class LongRunningGetIO extends AsyncTask<Void, Void, String> {		


		@Override
		protected String doInBackground(Void... params) {
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet get =
				    new HttpGet("http://www.cheesejedi.com/rest_services/get_big_cheese.php?puzzle=1");
		    get.setHeader("content-type", "application/json");
			
		    try
		    {
		            HttpResponse resp = httpClient.execute(get);
		            String respStr = EntityUtils.toString(resp.getEntity());
		     
					respJSON = new JSONArray(respStr);
		            
		            return "Ok";

		    }
		    catch(Exception ex)
		    {
		            Log.e("ServicioRest","Error!", ex);
		    }
			return null;
		}

		
		protected void onPostExecute(String results) {
			if (results.equalsIgnoreCase("Ok")) {
				
				try {
					
		            for(int i = 0; i < respJSON.length()-1 ; i++) {
		            	JSONObject elem = respJSON.getJSONObject(i);
		            	idList.add(elem.toString());
		            }
		            
					ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
							android.R.layout.simple_spinner_item, idList);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
					spnr.setAdapter(adapter);
					spnr.refreshDrawableState();
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
	}
	

}
